package com.broada.apm.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

/**
 * 支持保存分页信息的列表对象。
 * 
 * @author hesy
 * Create By 2012-5-30 下午03:29:56
 */
public class PagingList<T> {

	/**
	 * 分页查询时，开始页码
	 */
	private int startPage;
	
	/**
	 * 分页查询时，每页显示记录数
	 */
	private int pageSize;
	
	/**
	 * 分页查询时，总记录数
	 */
	private long totalRecords;
	
	/**
	 * 分页查询时，总页数
	 */
	private int totalPages;
	
	/**
	 * 查询结果数据
	 */
	private List<T> data = null;
	
	/**
	 * 查询时其他的参数
	 */
	private Map<String, Object> map = new HashMap<String, Object>();
	
	public PagingList() {
		data = new ArrayList<T>();
	}
	
	public PagingList(List<T> data) {
		if (data != null) {
			this.data = data;
		} else {
			this.data = new ArrayList<T>();
		}
	}
	
	/**
	 * 返回查询结果对象。
	 */
	public List<T> getData() {
		return data;
	}
	/**
	 * 返回查询结果对象。
	 */
	public void setData(List<T> data) {
		this.data=data;
	}
	public int getStartPage() {
		return startPage;
	}

	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public long getTotalRecords() {
		return totalRecords;
	}

	public void setTotalRecords(long totalRecords) {
		this.totalRecords = totalRecords;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	// ---------------- methods implements for List --------------------//
	public int size() {
		return data.size();
	}

	public boolean isEmpty() {
		return data.isEmpty();
	}

	public boolean contains(Object o) {
		return data.contains(o);
	}

	public Iterator<T> iterator() {
		return data.iterator();
	}

	public Object[] toArray() {
		return data.toArray();
	}

	@SuppressWarnings("hiding")
	public <T> T[] toArray(T[] a) {
		return data.toArray(a);
	}

	public boolean add(T o) {
		return data.add(o);
	}

	public boolean remove(Object o) {
		return data.remove(o);
	}

	public boolean containsAll(Collection<?> c) {
		return data.containsAll(c);
	}

	public boolean addAll(Collection<? extends T> c) {
		return data.addAll(c);
	}

	public boolean addAll(int index, Collection<? extends T> c) {
		return data.addAll(c);
	}

	public boolean removeAll(Collection<?> c) {
		return data.removeAll(c);
	}

	public boolean retainAll(Collection<?> c) {
		return data.retainAll(c);
	}

	public void clear() {
		data.clear();
	}

	public T get(int index) {
		return data.get(index);
	}

	public T set(int index, T element) {
		return data.set(index, element);
	}

	public void add(int index, T element) {
		data.add(index, element);
	}

	public T remove(int index) {
		return data.remove(index);
	}

	public int indexOf(Object o) {
		return data.indexOf(o);
	}

	public int lastIndexOf(Object o) {
		return data.lastIndexOf(o);
	}

	public ListIterator<T> listIterator() {
		return data.listIterator();
	}

	public ListIterator<T> listIterator(int index) {
		return data.listIterator(index);
	}

	public List<T> subList(int fromIndex, int toIndex) {
		return data.subList(fromIndex, toIndex);
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	/**
	 * 处理分页
	 * 
	 * @param pageIndex
	 * @param pageSize
	 * @param total
	 * @param result
	 */
	public static <T> void pagingHandler(int pageIndex, int pageSize, int total, PagingList<T> result) {
		int totalPage = 0;
		int number = total % pageSize;
		if (number > 0) {
			totalPage = total / pageSize + 1;
		} else {
			totalPage = total / pageSize;
		}
		result.setTotalPages(totalPage);
		result.setStartPage(pageIndex);
		result.setTotalRecords(total);
		result.setPageSize(pageSize);
	}
}